//
//  ZDMessageController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/20.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDMessageController.h"
#import "ZDTextViewCell.h"

static NSString *const ZDTextViewCellId = @"ZDTextViewCell";

@interface ZDMessageController ()

/**
 <#Description#>
 */
@property(nonatomic, copy) NSString *easting;
/**
 <#Description#>
 */
@property(nonatomic, copy) NSString *disease;
/**
 <#Description#>
 */
@property (nonatomic, assign) BOOL isEditing;
/**
 <#Description#>
 */
@property(nonatomic, strong) UIButton *rightButton;

@end

@implementation ZDMessageController

- (void)setupUI
{
    [super setupUI];
    self.title = @"个人健康信息";
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"编辑" forState:0];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    rightButton.frame = CGRectMake(0, 0, 50, 30);
    [rightButton addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = rightButton;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;

    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDTextViewCell" bundle:nil] forCellReuseIdentifier:ZDTextViewCellId];
}
- (void)loadData
{
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"1" forKey:@"flag"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:HealthyMessageUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [SVProgressHUD showInfoWithStatus:error];
        }else
        {
            weakSelf.easting = datas[@"eating_habits"];
            weakSelf.disease = datas[@"disease_history"];
        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
- (void)requestSave
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"2" forKey:@"flag"];
    [dict setObject:self.disease forKey:@"disease_history"];
    [dict setObject:self.easting forKey:@"eating_habits"];
    
    [SVProgressHUD showWithStatus:@"正在保存"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:HealthyMessageUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDTextViewCellId forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"饮食习惯";
        if (![self.easting isKindOfClass:[NSNull class]])
        {
            cell.textView.text = self.easting;
        }else{
            cell.textView.text = @"输入您的饮食习惯,忌口,(辣,油,盐...)";
        }
    }
    if (indexPath.row == 1) {
        cell.titleLabel.text = @"病史情况";
        if (![self.disease isKindOfClass:[NSNull class]])
        {
            cell.textView.text = self.disease;
        }else{
            cell.textView.text = @"输入您的主要病史情况,方便更好地为您服务...";
        }

    }
    return cell;
}
#pragma mark- 点击事件
- (void)rightClick
{
    ZDLog(@"%s---%d---编辑",__func__,__LINE__);
    self.isEditing = !self.isEditing;
    [self setEditing:_isEditing];
    NSString *title = self.isEditing ? @"保存" : @"编辑";
    [self.rightButton setTitle:title forState:0];
    if (!self.editing) {
        //去保存
        self.easting = [self getFieldText:0];
        self.disease = [self getFieldText:1];
        [self requestSave];
    }
    
}
- (void)setEditing:(BOOL)editing
{
    for (int i = 0; i < 2; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        ZDTextViewCell *cell = (ZDTextViewCell *)[self.tableView cellForRowAtIndexPath:path];
        cell.textView.editable = editing;
    }
}

- (NSString *)getFieldText:(NSInteger)indexPathRow
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:indexPathRow inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    if ([cell isKindOfClass:[ZDTextViewCell class]])
    {
        ZDTextViewCell *cells = (ZDTextViewCell *)cell;
        return  cells.textView.text;
    }
    return @"";
}
@end
